package com.atm.atmsys.handler;

import com.atm.atmsys.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionsHandler {


    @ExceptionHandler
    public Result exceptionHandler(SQLIntegrityConstraintViolationException e) {
        String message=e.getMessage();
        if(message.contains("Duplicate entry")){
            String[] s = message.split(" ");
            String adminName = s[2];
            return Result.error("该管理员已存在：" +adminName);
        }else {
            return Result.error("未知错误");
        }
    }

}
